package com.na.onlineservey

import com.na.onlineservey.questionansweringoption.CreateQuestionAnsweringOptionAction
import com.na.onlineservey.questionansweringoption.DeleteQuestionAnsweringOptionAction
import com.na.onlineservey.questionansweringoption.ListQuestionAnsweringOptionAction
import com.na.onlineservey.questionansweringoption.UpdateQuestionAnsweringOptionAction
import com.na.onlineservey.questionansweringoption.ReadQuestionAnsweringOptionAction
import com.na.onlineservey.questionansweringoption.SearchQuestionAnsweringOptionAction

import com.docu.common.Message
import org.springframework.beans.factory.annotation.Autowired
import grails.converters.JSON

class QuestionAnsweringOptionController {

    @Autowired
    private CreateQuestionAnsweringOptionAction createQuestionAnsweringOptionAction
    @Autowired
    private UpdateQuestionAnsweringOptionAction updateQuestionAnsweringOptionAction
    @Autowired
    private ListQuestionAnsweringOptionAction listQuestionAnsweringOptionAction
    @Autowired
    private DeleteQuestionAnsweringOptionAction deleteQuestionAnsweringOptionAction
    @Autowired
    private ReadQuestionAnsweringOptionAction readQuestionAnsweringOptionAction
    @Autowired
    private SearchQuestionAnsweringOptionAction searchQuestionAnsweringOptionAction

    static allowedMethods = [create: "POST", update: "POST", delete: "POST"]

    def list = {
        List list = listQuestionAnsweringOptionAction.execute(params, null)
        render listQuestionAnsweringOptionAction.postCondition(null, list) as JSON
    }

    def show = {
        QuestionAnsweringOption questionAnsweringOptionInstance = new QuestionAnsweringOption()
        render(template: "show", model: [questionAnsweringOptionInstance: questionAnsweringOptionInstance])
    }

    def create = {
        QuestionAnsweringOption questionAnsweringOptionInstance = new QuestionAnsweringOption(params)
        QuestionAnsweringOption questionAnsweringOptionInstanceInstance = createQuestionAnsweringOptionAction.preCondition(null, questionAnsweringOptionInstance)
        Message message = null
        if (questionAnsweringOptionInstanceInstance == null) {
            message = createQuestionAnsweringOptionAction.getValidationErrorMessageForUI(questionAnsweringOptionInstance)
        } else {
            questionAnsweringOptionInstanceInstance = createQuestionAnsweringOptionAction.execute(null, questionAnsweringOptionInstanceInstance)
            if (questionAnsweringOptionInstanceInstance) {
                message = createQuestionAnsweringOptionAction.getSuccessMessageForUI(questionAnsweringOptionInstanceInstance, createQuestionAnsweringOptionAction.SUCCESS_SAVE)
            } else {
                message = createQuestionAnsweringOptionAction.getErrorMessageForUI(questionAnsweringOptionInstance, createQuestionAnsweringOptionAction.FAIL_SAVE)
            }
        }
        render message as JSON
    }

    def edit = {
        render readQuestionAnsweringOptionAction.execute(params, null) as JSON
    }

    def update = {
        QuestionAnsweringOption questionAnsweringOptionInstance = new QuestionAnsweringOption(params)
        Object object = updateQuestionAnsweringOptionAction.preCondition(params, null)
        Message message = null
        if (object == false) {
            message = updateQuestionAnsweringOptionAction.getValidationErrorMessageForUI(questionAnsweringOptionInstance)
        } else {
            int noOfRows = (int) updateQuestionAnsweringOptionAction.execute(null, object)
            if (noOfRows > 0) {
                message = updateQuestionAnsweringOptionAction.getSuccessMessageForUI(questionAnsweringOptionInstance, updateQuestionAnsweringOptionAction.SUCCESS_UPDATE)
            } else {
                message = updateQuestionAnsweringOptionAction.getErrorMessageForUI(questionAnsweringOptionInstance, updateQuestionAnsweringOptionAction.FAIL_UPDATE)
            }
        }
        render message as JSON
    }

    def delete = {
        QuestionAnsweringOption questionAnsweringOptionInstance = new QuestionAnsweringOption(params)
        questionAnsweringOptionInstance.id = Long.parseLong(params.id)
        questionAnsweringOptionInstance.version = Long.parseLong(params.version)
        Message message = null
        if (questionAnsweringOptionInstance) {
            int rowCount = (int) deleteQuestionAnsweringOptionAction.execute(null, questionAnsweringOptionInstance)
            if (rowCount > 0) {
                message = deleteQuestionAnsweringOptionAction.getSuccessMessageForUI(questionAnsweringOptionInstance, deleteQuestionAnsweringOptionAction.SUCCESS_DELETE)
            } else {
                message = deleteQuestionAnsweringOptionAction.getErrorMessageForUI(questionAnsweringOptionInstance, deleteQuestionAnsweringOptionAction.FAIL_DELETE)
            }
        }
        else {
            message = deleteQuestionAnsweringOptionAction.getErrorMessageForUI(questionAnsweringOptionInstance, deleteQuestionAnsweringOptionAction.ALREADY_DELETED)
        }
        render message as JSON
    }

    def search = {
        QuestionAnsweringOption questionAnsweringOptionInstance = searchQuestionAnsweringOptionAction.execute(params.fieldName, params.fieldValue)
        if (questionAnsweringOptionInstance) {
            render questionAnsweringOptionInstance as JSON
        }
        else {
            render ''
        }

    }
}
